package menu.to.role.menu

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.alibaba.fastjson.TypeReference
import com.alibaba.fastjson.parser.Feature
import fundation.gsonPrint
import org.apache.commons.io.FileUtils
import org.apache.commons.lang3.StringUtils
import org.junit.Test
import java.io.File

/**
 * @author futao
 * Created on 2017/12/5 - 23:59.
 */
class Translate {
    private fun String.toOrderedMap(): LinkedHashMap<String, String> {
        if (this.isNullOrBlank()) throw Exception()
        return JSON.parseObject(this, object : TypeReference<LinkedHashMap<String, String>>() {}, Feature.OrderedField)
    }

    private fun getRightCache(controllerName: String): JSONObject? {
        return JSONObject()
    }

    @Test
    fun translate() {
//        menuString: String, roleString: String
        val menuString = menuString
        val roleString = roleString
        val roleType = 1

//        val jjj = JSON.parseObject(menuString)

        val role = JSONObject()

        val jjj = JSON.parseObject(FileUtils.readFileToString(File("C:\\Users\\11851\\Desktop\\menu.json")))
        jjj.forEach { (t, u) ->
            println(t)
            val u = u as JSONObject
            //action code is hear
            val role_u = JSONObject()
            role.put(t, role_u)
            //over
            if (u.containsKey("menuMap")) {
                val role_menuMap = JSONObject()
                role_u.put("menuMap", role_menuMap)
                u.getJSONObject("menuMap").forEach { (t2, u2) ->
                    val u2 = u2 as JSONObject
                    val role_u2 = JSONObject()
                    role_menuMap.put(t2, role_u2)
                    println("\t${t2}")

                    //action code is hear
                    if (u2.containsKey("ctrl")) {
                        var ctrl = u2.getJSONObject("ctrl")
                        if (ctrl.containsKey("auths")) {
                            val role_auth = JSONArray()
                            val auths = ctrl.getJSONArray("auths")
                            role_u2.put("auth", role_auth)
                            auths.forEach { _auth ->
                                if (_auth !is JSONObject) return@forEach
                                val auth = _auth as JSONObject
                                println("\t\t key:${auth.getString("key")} title:${auth.getString("title")} val:${auth.getString("val")}")
                            }
                        }
                    }


                    //over
                    if (u2.containsKey("items")) {
                        u2.getJSONArray("items").forEach { u3 ->
                            val u3 = u3 as JSONObject
                            println("\t\t${u3.getString("title")}")

                            //action code is hear
                            //over

                        }
                    }

                }
            }
        }
        println(StringUtils.repeat('=', 80))
        println(JSON.toJSONString(role, true))

        return
        val menuLevelOneMap = menuString.toOrderedMap()
        /*role一级菜单Map*/
        val roleMenuLevelOneMap = LinkedHashMap<Any, Any?>()

        menuLevelOneMap.forEach f1@ { (menuLevelOne_k, menuLevelOne_v) ->
            kotlin.run {
                val menuLevelTwoMap = menuLevelOne_v.toOrderedMap()
                /*role二级菜单Map*/
                val roleMenuLevelTwoMap = LinkedHashMap<Any, Any>()
                val menuLevelTwoMenu = menuLevelTwoMap["menu"]
                kotlin.run {
                    val menuLevelTwoContentMap = menuLevelTwoMenu?.toOrderedMap()
                    val isNav = menuLevelTwoContentMap?.let { it["nav"] }
                    if (isNav == "true") {

                        if (menuLevelTwoContentMap.containsKey("roleType")) {
                            val roleType4ThisMenu = menuLevelTwoContentMap.let { it["roleType"] }
                            if (roleType4ThisMenu == roleType.toString()) {
                                /*role menu content map*/
                                val roleMenuLevelThreeMap = LinkedHashMap<Any, Any?>()
                                with(roleMenuLevelThreeMap) {
                                    put("name", menuLevelTwoContentMap["name"])
                                    put("nav", menuLevelTwoContentMap["nav"])
                                    put("title", menuLevelTwoContentMap["title"])
                                    put("roleType", menuLevelTwoContentMap["roleType"])
                                    menuLevelTwoContentMap["style"]?.let { put("style", it) }
                                    menuLevelTwoContentMap["line"]?.let { put("line", it) }
                                    menuLevelTwoContentMap["icon"]?.let { put("icon", it) }
                                }
                                val menuLevelTwoCtrl = menuLevelTwoContentMap["ctrl"]
                                if (menuLevelTwoCtrl != null) {
                                    val methods = getRightCache(menuLevelTwoCtrl)
                                    methods?.let {
                                        val methodArray = JSON.parseArray(methods.getString("items"))
                                        val authArray = ArrayList<Any>()
                                        methodArray.forEach { method ->
                                            kotlin.run {
                                                val methodItem = method.toString().toOrderedMap()
                                                val methodName = methodItem["name"]
                                                val authArray = JSON.parseArray(roleString.toOrderedMap()[menuLevelOne_k]?.let { it.toOrderedMap()["menu"] }?.let { it.toOrderedMap()["auth"] })
                                                authArray.forEach { auth ->
                                                    kotlin.run {
                                                        if (auth.toString().toOrderedMap()["key"] == methodName) {
                                                            val auth = LinkedHashMap<Any, Any?>().apply {
                                                                put("key", methodName)
                                                                put("title", methodItem["viewName"])
                                                                put("val", auth.toString().toOrderedMap()["val"])
                                                            }
                                                            authArray.add(auth)
                                                        } else {
                                                            val auth = LinkedHashMap<Any, Any?>().apply {
                                                                put("key", methodName)
                                                                put("title", methodItem["viewName"])
                                                                put("val", false)
                                                            }
                                                            authArray.add(auth)
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        roleMenuLevelThreeMap.put("auth", authArray)
                                    }
                                } else {

                                }
                                roleMenuLevelTwoMap.put("menu", roleMenuLevelThreeMap)
                            } else {
                                return@f1
                            }
                        } else {
                            /*role menu content map*/
                            val roleMenuLevelThreeMap = LinkedHashMap<Any, Any?>()
                            with(roleMenuLevelThreeMap) {
                                put("name", menuLevelTwoContentMap["name"])
                                put("nav", menuLevelTwoContentMap["nav"])
                                put("title", menuLevelTwoContentMap["title"])
                                menuLevelTwoContentMap["style"]?.let { put("style", it) }
                                menuLevelTwoContentMap["line"]?.let { put("line", it) }
                                menuLevelTwoContentMap["icon"]?.let { put("icon", it) }
                            }
                            val menuLevelTwoCtrl = menuLevelTwoContentMap["ctrl"]
                            if (menuLevelTwoCtrl != null) {
                                val methods = getRightCache(menuLevelTwoCtrl)
                                methods?.let {
                                    val methodArray = JSON.parseArray(methods.getString("items"))
                                    val authArray = ArrayList<Any>()
                                    methodArray.forEach { method ->
                                        kotlin.run {
                                            val methodItem = method.toString().toOrderedMap()
                                            val methodName = methodItem["name"]
                                            val authArray = JSON.parseArray(roleString.toOrderedMap()[menuLevelOne_k]?.let { it.toOrderedMap()["menu"] }?.let { it.toOrderedMap()["auth"] })
                                            authArray.forEach { auth ->
                                                kotlin.run {
                                                    if (auth.toString().toOrderedMap()["key"] == methodName) {
                                                        val auth = LinkedHashMap<Any, Any?>().apply {
                                                            put("key", methodName)
                                                            put("title", methodItem["viewName"])
                                                            put("val", auth.toString().toOrderedMap()["val"])
                                                        }
                                                        authArray.add(auth)
                                                    } else {
                                                        val auth = LinkedHashMap<Any, Any?>().apply {
                                                            put("key", methodName)
                                                            put("title", methodItem["viewName"])
                                                            put("val", false)
                                                        }
                                                        authArray.add(auth)
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    roleMenuLevelThreeMap.put("auth", authArray)
                                }
                            } else {
                                /*ctrl为空*/
                            }
                            roleMenuLevelTwoMap.put("menu", roleMenuLevelThreeMap)
                        }
                    }
                    /*nav=false*/
                    else {
                        return@f1
                    }
                }

                val menuLevelTwoMenuMap = menuLevelTwoMap["menuMap"]
                kotlin.run {
                    val menuLevelTwoMenuMap = menuLevelTwoMenuMap?.toOrderedMap()
                    menuLevelTwoMenuMap?.forEach f2@ { (menuMap_key, menuMap_value) ->
                        kotlin.run {
                            val menuLevelThreeMap = menuMap_value.toOrderedMap()
                            /*role lv3 map*/
                            val roleMenuLevelThreeMap = LinkedHashMap<Any, Any?>()
                            if (menuLevelThreeMap["hide"] == "true") {
                                return@f2
                            }
                            /*hide=false*/
                            else {
                                if (menuLevelThreeMap["roleType"] != roleType.toString()) {
                                    return@f2
                                }
                                /*roleType = currentRoleType*/
                                else {
                                    with(roleMenuLevelThreeMap) {
                                        put("id", menuLevelThreeMap["id"])
                                        put("title", menuLevelThreeMap["title"])
                                        put("hide", menuLevelThreeMap["hide"])
                                        put("roleType", menuLevelThreeMap["roleType"])
                                        menuLevelThreeMap["style"]?.let { put("style", it) }
                                        menuLevelThreeMap["line"]?.let { put("line", it) }
                                        menuLevelThreeMap["icon"]?.let { put("icon", it) }
                                    }
                                    /*ctrl!=null*/
                                    if (menuLevelThreeMap["ctrl"] != null) {
                                        JSON.parseArray(menuLevelThreeMap["ctrl"]?.let { getRightCache(it) }?.getString("items")).forEach { ctrlItem ->
                                            kotlin.run {
                                                val auths = roleString.toOrderedMap()[menuLevelOne_k]?.let { it.toOrderedMap()["menuMap"] }?.let { it.toOrderedMap()[menuMap_key] }?.let { it.toOrderedMap()["auth"] }
                                                val authArray = JSON.parseArray(auths)
                                                val roleAuthArray = ArrayList<Any>()
                                                authArray.forEach { auth ->
                                                    kotlin.run {
                                                        val authContentMap = auth.toString().toOrderedMap()
                                                        val ctrlContentMap = ctrlItem.toString().toOrderedMap()
                                                        val authMap = LinkedHashMap<Any, Any?>()
                                                        if (authContentMap["key"] == ctrlContentMap["name"]) {
                                                            with(authMap) {
                                                                put("key", ctrlContentMap["key"])
                                                                put("title", ctrlContentMap["title"])
                                                                put("val", authContentMap["val"])
                                                            }
                                                        } else {
                                                            with(authMap) {
                                                                put("key", ctrlContentMap["key"])
                                                                put("title", ctrlContentMap["title"])
                                                                put("val", ctrlContentMap["val"])
                                                            }
                                                        }
                                                        roleAuthArray.add(authMap)
                                                    }
                                                }
                                                roleMenuLevelThreeMap.put("auth", roleAuthArray)
                                            }
                                        }
                                    }
                                    /*ctrl=null*/
                                    else {

                                    }
                                }
                            }


                        }
                    }
                }
                roleMenuLevelOneMap.put(menuLevelOne_k, menuLevelTwoMenuMap)
            }
        }
        gsonPrint(roleMenuLevelOneMap)
    }
}